<template>
    <div class="dashboard-container">
      <h2 class="dashboard-title">驾校考试系统 - 管理面板</h2>
      
      <div class="main-content">
        <!-- 快捷入口 -->
        <el-card class="quick-access">
          <template #header>
            <div class="card-header">
              <span>快捷入口</span>
            </div>
          </template>
          <div class="access-grid">
            <div 
              v-for="(item, index) in quickLinks" 
              :key="index"
              class="access-item"
              @click="navigateTo(item.path)"
            >
              <div class="access-icon" :style="{ backgroundColor: item.color }">
                <component :is="item.icon" />
              </div>
              <span class="access-label">{{ item.label }}</span>
            </div>
          </div>
        </el-card>
        
        <!-- 公告管理 -->
        <el-card class="announcement-card">
          <template #header>
            <div class="card-header">
              <span>系统公告</span>
              <el-button 
                type="primary" 
                size="small" 
                @click="openAnnouncementDialog(false)"
              >
                <el-icon><Plus /></el-icon>
                发布公告
              </el-button>
            </div>
          </template>
          <el-scrollbar height="300px">
            <div class="announcement-list">
              <div 
                v-for="(item, index) in announcements" 
                :key="index"
                class="announcement-item"
                :class="{ 'unread': item.isNew }"
              >
                <div class="announcement-badge" :style="{ backgroundColor: item.typeColor }"></div>
                <div class="announcement-content">
                  <h3 class="announcement-title">{{ item.title }}</h3>
                  <p class="announcement-text">{{ item.content }}</p>
                  <div class="announcement-footer">
                    <span class="announcement-time">
                      <el-icon><Clock /></el-icon>
                      {{ item.time }}
                    </span>
                    <span class="announcement-author">
                      <el-icon><User /></el-icon>
                      {{ item.author }}
                    </span>
                  </div>
                </div>
                <div class="announcement-actions">
                  <el-tooltip content="编辑" placement="top">
                    <el-button 
                      type="text" 
                      size="small" 
                      @click.stop="openAnnouncementDialog(true, index)"
                    >
                      <el-icon><Edit /></el-icon>
                    </el-button>
                  </el-tooltip>
                  <el-tooltip content="删除" placement="top">
                    <el-button 
                      type="text" 
                      size="small" 
                      @click.stop="deleteAnnouncement(index)"
                    >
                      <el-icon><Delete /></el-icon>
                    </el-button>
                  </el-tooltip>
                </div>
              </div>
            </div>
          </el-scrollbar>
        </el-card>
        
        <!-- 考试数据统计 -->
        <el-card class="stats-card">
          <template #header>
            <div class="card-header">
              <span>考试数据统计</span>
              <el-select v-model="timeRange" size="small" style="width: 120px">
                <el-option label="近7天" value="7days"></el-option>
                <el-option label="近30天" value="30days"></el-option>
                <el-option label="近90天" value="90days"></el-option>
              </el-select>
            </div>
          </template>
          <div class="chart-container">
            <canvas id="statsChart"></canvas>
          </div>
        </el-card>
      </div>
      
      <!-- 公告编辑对话框 -->
      <el-dialog
        v-model="showAnnouncementDialog"
        :title="isEditing ? '编辑公告' : '发布公告'"
        width="600px"
      >
        <el-form :model="currentAnnouncement" label-width="80px">
          <el-form-item label="公告标题">
            <el-input v-model="currentAnnouncement.title" placeholder="请输入公告标题" />
          </el-form-item>
          <el-form-item label="公告类型">
            <el-select v-model="currentAnnouncement.type" placeholder="请选择公告类型">
              <el-option 
                v-for="item in announcementTypes"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
                <span style="float: left">{{ item.label }}</span>
                <span 
                  style="float: right; color: var(--el-text-color-secondary); font-size: 12px"
                >
                  {{ item.desc }}
                </span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="公告内容">
            <el-input
              v-model="currentAnnouncement.content"
              type="textarea"
              :rows="5"
              placeholder="请输入公告内容"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="showAnnouncementDialog = false">取消</el-button>
          <el-button type="primary" @click="saveAnnouncement">保存</el-button>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { ref, onMounted } from 'vue'
  import { useRouter } from 'vue-router'
  import Chart from 'chart.js/auto'
  import { 
    User, Document, Trophy, Calendar, Plus, 
    Clock, Edit, Delete, UserFilled, Setting,
    Files, EditPen, CircleCheck, Notebook
  } from '@element-plus/icons-vue'
  
  export default {
    name: 'Dashboard',
    components: {
      User, Document, Trophy, Calendar, Plus,
      Clock, Edit, Delete, UserFilled, Setting,
      Files, EditPen, CircleCheck, Notebook
    },
    setup() {
      const router = useRouter()
      
      // 快捷入口数据
      const quickLinks = ref([
        { 
          label: '学员管理', 
          path: '/students', 
          icon: 'UserFilled',
          color: '#409EFF'
        },
        { 
          label: '单选题', 
          path: '/single-choice', 
          icon: 'CircleCheck',
          color: '#67C23A'
        },
        { 
          label: '判断题', 
          path: '/judgement', 
          icon: 'Check',
          color: '#E6A23C'
        },
        { 
          label: '简答题', 
          path: '/short-answer', 
          icon: 'EditPen',
          color: '#909399'
        },
        { 
          label: '考试试卷', 
          path: '/exam-papers', 
          icon: 'Files',
          color: '#F56C6C'
        },
        { 
          label: '考试设置', 
          path: '/question-settings', 
          icon: 'Setting',
          color: '#409EFF'
        }
      ])
      
      // 公告数据
      const announcements = ref([
        { 
          title: '2024年第三季度考试成绩发布', 
          content: '2024年第三季度各科目考试成绩已发布，请学员及时登录系统查询。', 
          time: '2024-10-15 09:30',
          author: '张管理员',
          type: 'notice',
          typeColor: '#409EFF',
          isNew: true
        },
        { 
          title: '系统维护通知', 
          content: '系统将于10月20日22:00至24:00进行维护升级，届时将无法访问，请提前做好安排。', 
          time: '2024-10-10 14:15',
          author: '李管理员',
          type: 'warning',
          typeColor: '#E6A23C',
          isNew: false
        },
        { 
          title: '新增科目四模拟试题', 
          content: '题库新增500道科目四模拟试题，涵盖最新交通法规内容，请学员及时练习。', 
          time: '2024-09-28 16:45',
          author: '王管理员',
          type: 'info',
          typeColor: '#909399',
          isNew: false
        }
      ])
      
      // 公告类型选项
      const announcementTypes = ref([
        { value: 'notice', label: '通知公告', desc: '一般性通知' },
        { value: 'warning', label: '重要通知', desc: '需要特别注意' },
        { value: 'info', label: '信息公告', desc: '普通信息' }
      ])
      
      // 当前编辑的公告
      const currentAnnouncement = ref({
        title: '',
        content: '',
        type: 'notice'
      })
      
      // 对话框状态
      const showAnnouncementDialog = ref(false)
      const isEditing = ref(false)
      const editIndex = ref(null)
      const timeRange = ref('7days')
      
      // 导航方法
      const navigateTo = (path) => {
        router.push(path)
      }
      
      // 打开公告对话框
      const openAnnouncementDialog = (isEdit, index = null) => {
        isEditing.value = isEdit
        editIndex.value = index
        
        if (isEdit) {
          currentAnnouncement.value = { ...announcements.value[index] }
        } else {
          currentAnnouncement.value = { title: '', content: '', type: 'notice' }
        }
        
        showAnnouncementDialog.value = true
      }
      
      // 保存公告
      const saveAnnouncement = () => {
        if (!currentAnnouncement.value.title || !currentAnnouncement.value.content) {
          return
        }
        
        const now = new Date()
        const timeStr = `${now.getFullYear()}-${(now.getMonth()+1).toString().padStart(2, '0')}-${now.getDate().toString().padStart(2, '0')} ${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`
        
        const typeColorMap = {
          notice: '#409EFF',
          warning: '#E6A23C',
          info: '#909399'
        }
        
        const newAnnouncement = {
          ...currentAnnouncement.value,
          time: timeStr,
          author: '当前管理员',
          typeColor: typeColorMap[currentAnnouncement.value.type] || '#409EFF',
          isNew: true
        }
        
        if (isEditing.value) {
          announcements.value[editIndex.value] = newAnnouncement
        } else {
          announcements.value.unshift(newAnnouncement)
        }
        
        showAnnouncementDialog.value = false
      }
      
      // 删除公告
      const deleteAnnouncement = (index) => {
        announcements.value.splice(index, 1)
      }
      
      // 初始化图表
      onMounted(() => {
        const ctx = document.getElementById('statsChart')
        
        new Chart(ctx, {
          type: 'line',
          data: {
            labels: ['10/1', '10/2', '10/3', '10/4', '10/5', '10/6', '10/7'],
            datasets: [
              {
                label: '科目一通过率',
                data: [85, 82, 88, 90, 87, 89, 91],
                borderColor: '#409EFF',
                backgroundColor: 'rgba(64, 158, 255, 0.1)',
                tension: 0.3,
                fill: true
              },
              {
                label: '科目二通过率',
                data: [78, 80, 82, 85, 83, 84, 86],
                borderColor: '#67C23A',
                backgroundColor: 'rgba(103, 194, 58, 0.1)',
                tension: 0.3,
                fill: true
              },
              {
                label: '科目三通过率',
                data: [82, 83, 85, 84, 86, 87, 88],
                borderColor: '#E6A23C',
                backgroundColor: 'rgba(230, 162, 60, 0.1)',
                tension: 0.3,
                fill: true
              }
            ]
          },
          options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
              legend: {
                position: 'top'
              },
              tooltip: {
                mode: 'index',
                intersect: false
              }
            },
            scales: {
              y: {
                beginAtZero: false,
                min: 70,
                max: 100,
                ticks: {
                  callback: function(value) {
                    return value + '%'
                  }
                }
              }
            }
          }
        })
      })
      
      return {
        quickLinks,
        announcements,
        announcementTypes,
        currentAnnouncement,
        showAnnouncementDialog,
        isEditing,
        timeRange,
        navigateTo,
        openAnnouncementDialog,
        saveAnnouncement,
        deleteAnnouncement
      }
    }
  }
  </script>
  
  <style scoped>
  .dashboard-container {
    padding: 20px;
  }
  
  .dashboard-title {
    font-size: 24px;
    color: #333;
    margin-bottom: 24px;
  }
  
  .overview-cards {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
    margin-bottom: 24px;
  }
  
  .overview-card {
    border-radius: 8px;
    transition: all 0.3s;
  }
  
  .overview-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }
  
  .card-content {
    display: flex;
    align-items: center;
    padding: 16px;
  }
  
  .card-icon {
    font-size: 36px;
    margin-right: 16px;
    color: #409EFF;
  }
  
  .card-info {
    flex: 1;
  }
  
  .card-title {
    font-size: 14px;
    color: #909399;
    margin-bottom: 8px;
  }
  
  .card-value {
    font-size: 24px;
    font-weight: 600;
    color: #303133;
  }
  
  .main-content {
    display: grid;
    grid-template-columns: repeat(12, 1fr);
    gap: 20px;
  }
  
  .quick-access {
    grid-column: span 4;
  }
  
  .announcement-card {
    grid-column: span 4;
  }
  
  .stats-card {
    grid-column: span 4;
  }
  
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  
  .access-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
  }
  
  .access-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 16px;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s;
    border: 1px solid #ebeef5;
  }
  
  .access-item:hover {
    background-color: #f5f7fa;
    transform: translateY(-3px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }
  
  .access-icon {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 12px;
    color: white;
    font-size: 24px;
  }
  
  .access-label {
    font-size: 14px;
    color: #606266;
    font-weight: 500;
  }
  
  .announcement-list {
    padding: 8px 0;
  }
  
  .announcement-item {
    display: flex;
    padding: 16px 0;
    border-bottom: 1px solid #ebeef5;
    position: relative;
  }
  
  .announcement-item:last-child {
    border-bottom: none;
  }
  
  .announcement-badge {
    position: absolute;
    left: 0;
    top: 20px;
    width: 4px;
    height: 40px;
    border-radius: 2px;
  }
  
  .announcement-content {
    flex: 1;
    margin-left: 16px;
  }
  
  .announcement-title {
    font-size: 15px;
    font-weight: 500;
    color: #303133;
    margin: 0 0 8px 0;
  }
  
  .announcement-text {
    font-size: 13px;
    color: #606266;
    margin: 0 0 8px 0;
    line-height: 1.5;
  }
  
  .announcement-footer {
    display: flex;
    font-size: 12px;
    color: #909399;
  }
  
  .announcement-time {
    margin-right: 15px;
    display: flex;
    align-items: center;
  }
  
  .announcement-time .el-icon {
    margin-right: 4px;
    font-size: 12px;
  }
  
  .announcement-author {
    display: flex;
    align-items: center;
  }
  
  .announcement-author .el-icon {
    margin-right: 4px;
    font-size: 12px;
  }
  
  .announcement-actions {
    margin-left: 10px;
  }
  
  .chart-container {
    height: 300px;
    position: relative;
  }
  
  .unread {
    background-color: #f5f7fa;
  }
  
  /* 响应式调整 */
  @media (max-width: 1200px) {
    .overview-cards {
      grid-template-columns: repeat(2, 1fr);
    }
    
    .quick-access,
    .announcement-card,
    .stats-card {
      grid-column: span 6;
    }
  }
  
  @media (max-width: 768px) {
    .overview-cards {
      grid-template-columns: 1fr;
    }
    
    .main-content {
      grid-template-columns: 1fr;
    }
    
    .quick-access,
    .announcement-card,
    .stats-card {
      grid-column: span 1;
    }
    
    .access-grid {
      grid-template-columns: 1fr;
    }
  }
  </style>